<%@ page import="com.generalynx.common.constants.WebConst"%>

var currentNode = null;

function clickPM(treeid, nodeid, plus, minus)
{
    myUl = document.getElementById('div'+treeid+'_'+nodeid);
    if (myUl)
    {
        if (myUl.style.display != 'block')
        {
//            if (currentNode != null)
//                collapseNode(treeid, currentNode, plus);
            expandNode(treeid, nodeid, minus);
            currentNode = nodeid;
        }
        else
            collapseNode(treeid, nodeid, plus);
    }
    else
      alert("No element for tree " + treeid + " with id " + nodeid);
}

function getNode(treeid, nodeid)
{
    return document.getElementById('div'+treeid+'_'+nodeid);
}

function getChkBox(treeid, nodeid)
{
    return document.getElementById("inputChk" + treeid + '_' + nodeid);
}

function expandNode(treeid, nodeid, minus)
{
    var myUl = getNode(treeid, nodeid);
    var expChkBox = getChkBox(treeid, nodeid);
    if (myUl)
    {
        if (expChkBox)
            expChkBox.checked = true;
        myImg = document.getElementById('img'+treeid+'_'+nodeid);
        myUl.style.display = 'block';
        if (myImg) {
            myImg.src = minus;
        }
    }
}

function collapseNode(treeid, nodeid, plus) {
    var myUl = getNode(treeid, nodeid);
    var expChkBox = getChkBox(treeid, nodeid);
    if (myUl) {
        if (expChkBox)
          expChkBox.checked = false;
        myImg = document.getElementById('img'+treeid+'_'+nodeid);
        myUl.style.display = 'none';
        if (myImg) {
            myImg.src = plus;
        }
//        unsetCheckBoxes();
    }
}

function setAllCheckBoxes(set) {
    var elts = getAllLeafChks();
    var nElts = elts.length;
    var i;
    for (i = 0; i < nElts; i++)
        elts[i].checked = set;
}

function getAllLeafChks() {
    return document.getElementsByName('leaves');
}

var aSelectedNodeId = new Array();
function selectNode(treeid, nodeid) {
    if (node = getNodeSpan(treeid, nodeid)) {
        if (aSelectedNodeId[treeid])
            getNodeSpan(treeid, aSelectedNodeId[treeid]).className = 'nodename';
        aSelectedNodeId[treeid] = nodeid;
        node.className = 'selectedtreenode';
    }
}

function getNodeSpan(treeid, nodeid) {
    return document.getElementById('span'+treeid+'_'+nodeid);
}


function setImgSrc(imgid, imgsrc) {
    if (myimg = document.getElementById(imgid))
        myimg.src = imgsrc;
}

function openCheckedNodes(treeid, name, plus, minus) {
    var chks = document.getElementsByName(name);
    var strNodeId;
    var nodeId;
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            strNodeId = chks[i].id;
            nodeId = strNodeId.substr(strNodeId.indexOf('_') + 1, strNodeId.length);
            clickPM(treeid, nodeId, plus, minus);
        }
    }
}